namespace Terra.Core.Common.ErrorCodes.Constants;

using Terra.Core.Common.ErrorCodes.Attributes;
using Terra.Core.Common.ErrorCodes.Models;

/// <summary>
/// 安全相关错误码常量
/// </summary>
public static class SecurityErrorCodes
{
    /// <summary>
    /// 认证错误码前缀
    /// </summary>
    private const string AuthPrefix = "AUTH";

    #region 认证相关错误码

    /// <summary>
    /// 登录失败
    /// </summary>
    [ErrorCode("AUTH001", "登录失败", Severity = ErrorSeverity.Error)]
    public const string LoginFailed = $"{AuthPrefix}001";

    /// <summary>
    /// 未经授权的访问
    /// </summary>
    [ErrorCode("AUTH002", "未经授权的访问", Severity = ErrorSeverity.Error)]
    public const string Unauthorized = $"{AuthPrefix}002";

    /// <summary>
    /// 无效的刷新令牌
    /// </summary>
    [ErrorCode("AUTH003", "无效的刷新令牌", Severity = ErrorSeverity.Error)]
    public const string InvalidRefreshToken = $"{AuthPrefix}003";

    /// <summary>
    /// 刷新令牌已被撤销
    /// </summary>
    [ErrorCode("AUTH004", "刷新令牌已被撤销", Severity = ErrorSeverity.Error)]
    public const string RefreshTokenRevoked = $"{AuthPrefix}004";

    /// <summary>
    /// 刷新令牌已被使用
    /// </summary>
    [ErrorCode("AUTH005", "刷新令牌已被使用", Severity = ErrorSeverity.Error)]
    public const string RefreshTokenUsed = $"{AuthPrefix}005";

    /// <summary>
    /// 刷新令牌已过期
    /// </summary>
    [ErrorCode("AUTH006", "刷新令牌已过期", Severity = ErrorSeverity.Error)]
    public const string RefreshTokenExpired = $"{AuthPrefix}006";

    /// <summary>
    /// 令牌刷新失败
    /// </summary>
    [ErrorCode("AUTH007", "令牌刷新失败: {0}", Severity = ErrorSeverity.Error)]
    public const string RefreshTokenFailed = $"{AuthPrefix}007";

    /// <summary>
    /// 用户不存在
    /// </summary>
    [ErrorCode("AUTH008", "用户不存在", Severity = ErrorSeverity.Error)]
    public const string UserNotFound = $"{AuthPrefix}008";

    /// <summary>
    /// 密码修改失败
    /// </summary>
    [ErrorCode("AUTH009", "密码修改失败: {0}", Severity = ErrorSeverity.Error)]
    public const string ChangePasswordFailed = $"{AuthPrefix}009";

    #endregion

    #region 授权相关错误码

    /// <summary>
    /// 权限不足
    /// </summary>
    [ErrorCode("AUTH101", "权限不足", Severity = ErrorSeverity.Error)]
    public const string InsufficientPermissions = $"{AuthPrefix}101";

    /// <summary>
    /// 角色不存在
    /// </summary>
    [ErrorCode("AUTH102", "角色不存在", Severity = ErrorSeverity.Error)]
    public const string RoleNotFound = $"{AuthPrefix}102";

    /// <summary>
    /// 无效的权限声明
    /// </summary>
    [ErrorCode("AUTH103", "无效的权限声明", Severity = ErrorSeverity.Error)]
    public const string InvalidClaim = $"{AuthPrefix}103";

    #endregion

    #region 密码策略相关错误码

    /// <summary>
    /// 密码不符合复杂度要求
    /// </summary>
    [ErrorCode("AUTH201", "密码不符合复杂度要求", Severity = ErrorSeverity.Error)]
    public const string PasswordComplexityError = $"{AuthPrefix}201";

    /// <summary>
    /// 密码不能与历史密码相同
    /// </summary>
    [ErrorCode("AUTH202", "密码不能与历史密码相同", Severity = ErrorSeverity.Error)]
    public const string PasswordHistoryError = $"{AuthPrefix}202";

    /// <summary>
    /// 密码已过期
    /// </summary>
    [ErrorCode("AUTH203", "密码已过期，请修改密码", Severity = ErrorSeverity.Warning)]
    public const string PasswordExpired = $"{AuthPrefix}203";

    #endregion

    #region 账户安全相关错误码

    /// <summary>
    /// 账户已锁定
    /// </summary>
    [ErrorCode("AUTH301", "账户已锁定，请 {0} 分钟后重试", Severity = ErrorSeverity.Error)]
    public const string AccountLocked = $"{AuthPrefix}301";

    /// <summary>
    /// 账户已禁用
    /// </summary>
    [ErrorCode("AUTH302", "账户已禁用，请联系管理员", Severity = ErrorSeverity.Error)]
    public const string AccountDisabled = $"{AuthPrefix}302";

    /// <summary>
    /// 需要二次认证
    /// </summary>
    [ErrorCode("AUTH303", "需要进行二次认证", Severity = ErrorSeverity.Warning)]
    public const string RequiresTwoFactor = $"{AuthPrefix}303";

    /// <summary>
    /// IP地址已被封禁
    /// </summary>
    [ErrorCode("AUTH304", "IP地址已被封禁，请联系管理员", Severity = ErrorSeverity.Error)]
    public const string IpAddressBanned = $"{AuthPrefix}304";

    #endregion

    #region 会话相关错误码

    /// <summary>
    /// 会话已过期
    /// </summary>
    [ErrorCode("AUTH401", "会话已过期，请重新登录", Severity = ErrorSeverity.Error)]
    public const string SessionExpired = $"{AuthPrefix}401";

    /// <summary>
    /// 会话已在其他设备登录
    /// </summary>
    [ErrorCode("AUTH402", "账号已在其他设备登录", Severity = ErrorSeverity.Warning)]
    public const string SessionTakenOver = $"{AuthPrefix}402";

    /// <summary>
    /// 并发会话数超限
    /// </summary>
    [ErrorCode("AUTH403", "当前在线设备数已达上限", Severity = ErrorSeverity.Error)]
    public const string ConcurrentSessionLimitExceeded = $"{AuthPrefix}403";

    #endregion

    #region 注册相关错误码

    /// <summary>
    /// 用户名已存在
    /// </summary>
    [ErrorCode("AUTH501", "用户名已被使用")]
    public const string UserNameAlreadyExists = $"{AuthPrefix}501";

    /// <summary>
    /// 邮箱已存在
    /// </summary>
    [ErrorCode("AUTH502", "邮箱已被使用")]
    public const string EmailAlreadyExists = $"{AuthPrefix}502";

    /// <summary>
    /// 手机号已存在
    /// </summary>
    [ErrorCode("AUTH503", "手机号已被使用")]
    public const string PhoneNumberAlreadyExists = $"{AuthPrefix}503";

    /// <summary>
    /// 注册失败
    /// </summary>
    [ErrorCode("AUTH504", "注册失败，请稍后重试")]
    public const string RegistrationFailed = $"{AuthPrefix}504";

    #endregion

    #region 密码重置相关错误
    /// <summary>
    /// 用户未设置邮箱
    /// </summary>
    public const string EmailNotFound = "SEC4001";

    /// <summary>
    /// 用户未设置手机号
    /// </summary>
    public const string PhoneNotFound = "SEC4002";

    /// <summary>
    /// 未授权的操作
    /// </summary>
    public const string UnauthorizedOperation = "SEC4003";

    /// <summary>
    /// 无效的重置类型
    /// </summary>
    public const string InvalidResetType = "SEC4004";

    /// <summary>
    /// 密码重置失败
    /// </summary>
    public const string PasswordResetFailed = "SEC4005";

    /// <summary>
    /// 密码重置令牌无效或已过期
    /// </summary>
    public const string InvalidResetToken = "SEC4006";

    /// <summary>
    /// 密码强度不符合要求
    /// </summary>
    public const string PasswordStrengthInsufficient = "SEC4007";

    /// <summary>
    /// 密码不正确
    /// </summary>
    public const string InvalidCredentials = "SEC4008";

    public const string EmailSendFailed = "SEC4009";

    public const string SmsSendFailed = "SEC4010";
    #endregion

    #region 用户管理错误码 USER001-USER099

    // 用户管理错误码 USER001-USER099
    /// <summary>
    /// 用户不存在
    /// </summary>
    public const string USER_NOT_FOUND = "USER001";

    /// <summary>
    /// 用户名已存在
    /// </summary>
    public const string USER_NAME_ALREADY_EXISTS = "USER002";

    /// <summary>
    /// 邮箱已存在
    /// </summary>
    public const string USER_EMAIL_ALREADY_EXISTS = "USER003";

    /// <summary>
    /// 手机号已存在
    /// </summary>
    public const string USER_PHONE_ALREADY_EXISTS = "USER004";

    /// <summary>
    /// 密码格式不符合要求
    /// </summary>
    public const string USER_PASSWORD_INVALID = "USER005";

    /// <summary>
    /// 无法分配角色
    /// </summary>
    public const string USER_ROLE_ASSIGN_FAILED = "USER006";

    /// <summary>
    /// 用户已被锁定
    /// </summary>
    public const string USER_LOCKED = "USER007";

    /// <summary>
    /// 用户已被删除
    /// </summary>
    public const string USER_DELETED = "USER008";

    /// <summary>
    /// 系统用户不允许修改
    /// </summary>
    public const string SYSTEM_USER_MODIFY_FORBIDDEN = "USER009";

    /// <summary>
    /// 用户状态更新失败
    /// </summary>
    public const string USER_STATUS_UPDATE_FAILED = "USER010";

    #endregion

    #region 角色管理错误码 ROLE001-ROLE099
    /// <summary>
    /// 角色名称已存在
    /// </summary>
    [ErrorCode("ROLE001", "角色名称已存在", Severity = ErrorSeverity.Error)]
    public const string ROLE_NAME_ALREADY_EXISTS = "ROLE001";

    /// <summary>
    /// 角色不存在
    /// </summary>
    [ErrorCode("ROLE002", "角色不存在", Severity = ErrorSeverity.Error)]
    public const string ROLE_NOT_FOUND = "ROLE002";

    /// <summary>
    /// 系统角色不允许修改
    /// </summary>
    [ErrorCode("ROLE003", "系统角色不允许修改", Severity = ErrorSeverity.Error)]
    public const string SYSTEM_ROLE_MODIFY_FORBIDDEN = "ROLE003";

    /// <summary>
    /// 系统角色不允许删除
    /// </summary>
    [ErrorCode("ROLE004", "系统角色不允许删除", Severity = ErrorSeverity.Error)]
    public const string SYSTEM_ROLE_DELETE_FORBIDDEN = "ROLE004";

    /// <summary>
    /// 角色分配用户失败
    /// </summary>
    [ErrorCode("ROLE005", "角色分配用户失败: {0}", Severity = ErrorSeverity.Error)]
    public const string ROLE_ASSIGN_USERS_FAILED = "ROLE005";

    /// <summary>
    /// 角色并发冲突
    /// </summary>
    [ErrorCode("ROLE006", "角色已被其他用户修改，请刷新后重试", Severity = ErrorSeverity.Error)]
    public const string ROLE_CONCURRENCY_CONFLICT = "ROLE006";
    #endregion

    #region 菜单相关错误码
    /// <summary>
    /// 菜单不存在
    /// </summary>
    [ErrorCode("MENU001", "菜单不存在", Severity = ErrorSeverity.Error)]
    public const string MenuNotFound = "MENU001";

    /// <summary>
    /// 菜单名称已存在
    /// </summary>
    [ErrorCode("MENU002", "菜单名称已存在", Severity = ErrorSeverity.Error)]
    public const string MenuNameAlreadyExists = "MENU002";

    /// <summary>
    /// 菜单路径已存在
    /// </summary>
    [ErrorCode("MENU003", "菜单路径已存在", Severity = ErrorSeverity.Error)]
    public const string MenuPathAlreadyExists = "MENU003";

    /// <summary>
    /// 父菜单不存在
    /// </summary>
    [ErrorCode("MENU004", "父菜单不存在", Severity = ErrorSeverity.Error)]
    public const string ParentMenuNotFound = "MENU004";

    /// <summary>
    /// 不能将自己设置为父菜单
    /// </summary>
    [ErrorCode("MENU005", "不能将菜单本身设置为父菜单", Severity = ErrorSeverity.Error)]
    public const string CannotSetSelfAsParent = "MENU005";

    /// <summary>
    /// 菜单循环依赖
    /// </summary>
    [ErrorCode("MENU006", "菜单层级形成循环依赖", Severity = ErrorSeverity.Error)]
    public const string CircularDependencyFound = "MENU006";

    /// <summary>
    /// 菜单存在子菜单，无法删除
    /// </summary>
    [ErrorCode("MENU007", "菜单存在子菜单，请先删除子菜单", Severity = ErrorSeverity.Error)]
    public const string MenuHasChildren = "MENU007";

    /// <summary>
    /// 菜单类型无效
    /// </summary>
    [ErrorCode("MENU008", "菜单类型无效", Severity = ErrorSeverity.Error)]
    public const string InvalidMenuType = "MENU008";
    #endregion

    #region 部门管理错误码 DEPT001-DEPT099
    /// <summary>
    /// 部门不存在
    /// </summary>
    [ErrorCode("DEPT001", "部门不存在", Severity = ErrorSeverity.Error)]
    public const string DEPT_NOT_FOUND = "DEPT001";

    /// <summary>
    /// 部门名称已存在
    /// </summary>
    [ErrorCode("DEPT002", "部门名称已存在", Severity = ErrorSeverity.Error)]
    public const string DEPT_NAME_ALREADY_EXISTS = "DEPT002";

    /// <summary>
    /// 部门编码已存在
    /// </summary>
    [ErrorCode("DEPT003", "部门编码已存在", Severity = ErrorSeverity.Error)]
    public const string DEPT_CODE_ALREADY_EXISTS = "DEPT003";

    /// <summary>
    /// 父部门不存在
    /// </summary>
    [ErrorCode("DEPT004", "父部门不存在", Severity = ErrorSeverity.Error)]
    public const string PARENT_DEPT_NOT_FOUND = "DEPT004";

    /// <summary>
    /// 不能将自己设置为父部门
    /// </summary>
    [ErrorCode("DEPT005", "不能将部门本身设置为父部门", Severity = ErrorSeverity.Error)]
    public const string CANNOT_SET_SELF_AS_PARENT_DEPT = "DEPT005";

    /// <summary>
    /// 部门层级形成循环依赖
    /// </summary>
    [ErrorCode("DEPT006", "部门层级形成循环依赖", Severity = ErrorSeverity.Error)]
    public const string DEPT_CIRCULAR_DEPENDENCY = "DEPT006";

    /// <summary>
    /// 部门存在子部门，无法删除
    /// </summary>
    [ErrorCode("DEPT007", "部门存在子部门，请先删除或移动子部门", Severity = ErrorSeverity.Error)]
    public const string DEPT_HAS_CHILDREN = "DEPT007";

    /// <summary>
    /// 部门下存在用户，无法删除
    /// </summary>
    [ErrorCode("DEPT008", "部门下存在用户，请先移除关联用户", Severity = ErrorSeverity.Error)]
    public const string DEPT_HAS_USERS = "DEPT008";

    /// <summary>
    /// 部门状态更新失败
    /// </summary>
    [ErrorCode("DEPT009", "部门状态更新失败", Severity = ErrorSeverity.Error)]
    public const string DEPT_STATUS_UPDATE_FAILED = "DEPT009";
    #endregion

    #region 权限管理错误码 PERMISSION001-PERMISSION099
    /// <summary>
    /// 权限已存在
    /// </summary>
    [ErrorCode("PERMISSION001", "权限已存在", Severity = ErrorSeverity.Error)]
    public const string PERMISSION_ALREADY_EXISTS = "PERMISSION001";

    /// <summary>
    /// 权限不存在
    /// </summary>
    [ErrorCode("PERMISSION002", "权限不存在", Severity = ErrorSeverity.Error)]
    public const string PERMISSION_NOT_FOUND = "PERMISSION002";

    /// <summary>
    /// 权限已被使用
    /// </summary>
    [ErrorCode("PERMISSION003", "权限{0}已被使用，无法删除", Severity = ErrorSeverity.Error)]
    public const string PERMISSION_IN_USE = "PERMISSION003";

    #endregion


}